import { useSelector } from 'react-redux'
import { RootState } from '../store';
import { useAppSelector } from '../store/hooks';
export function useCar() {
	// 购物车数据
	const carList = useAppSelector((state) => state.car.carList);
	// 总数量
	const totalCount = carList.reduce((p, n) => {
		return p + n.count;
	}, 0);
	// 总价钱：商品必须被选中
	const totalPrice = carList.reduce((p, n) => {
		return n.isChecked ? p + n.price * n.count : p
	}, 0)
	// 全选状态
	const isTotalChecked = carList.length === 0 ? false : carList.every(v => v.isChecked)
	// 提交状态
	const isSubmit = carList.some(v => v.isChecked)
	return {
		carList,
		totalCount,
		totalPrice,
		isTotalChecked,
		isSubmit
	}
}